package com.lovo.sys.controller;

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONObject;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.lovo.commom.vo.ResultVO;
import com.lovo.sys.bean.UserBean;
import com.lovo.sys.service.IUserService;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.openfeign.SpringQueryMap;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("user")
public class UserController {
    @Autowired
    private IUserService userService;
    @Autowired
    private RabbitTemplate rabbitTemplate;

    @GetMapping("findByConsumption/{pageNO}")
    public IPage<UserBean> findByConsumption(@PathVariable("pageNO") int pageNO) {
        return userService.findByConsumption(pageNO);
    }

    @GetMapping("findById/{id}")
    public UserBean findById(@PathVariable("id") int id) {
        return userService.findById(id);
    }

    @GetMapping("sendFrozen")
    public ResultVO sendFrozen(int userId, String reason) {
        UserBean user = userService.findById(userId);
        //发送冻结申请
        String json = JSON.toJSONString(user) + "%" + reason;
        rabbitTemplate.convertAndSend("SSc_Sys_Dev", "rabbit.frozen", json);
        ResultVO resultVO = new ResultVO();
        resultVO.setTag(0);
        resultVO.setJson("发送冻结请求成功");
        return resultVO;
    }

    @GetMapping("updateFrozenState")
    public ResultVO updateFrozenState(@RequestParam("userName") String userName, @RequestParam("status") int status) {
        userService.updateFrozenState(userName, status);
        ResultVO resultVO = new ResultVO();
        resultVO.setTag(0);
        resultVO.setJson("成功");
        return resultVO;
    }

    @RequestMapping("add")
    public boolean add(@RequestBody String obj) {
        UserBean user = JSONObject.parseObject(obj, UserBean.class);
        userService.addUser(user);
        return true;
    }


}
